/*     */ package com.eptok.yspay.opensdkjava.util;
/*     */

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.logging.Logger;

/*     */
/*     */ public class HttpClientUtil {
/*  15 */   private static final Logger logger = Logger.getLogger("HttpClientUtil");
/*     */
/*     */
/*     */
/*     */
/*     */   public static String sendPostJson(String url, String param) throws IOException {
/*  21 */     PrintWriter out = null;
/*  22 */     BufferedReader in = null;
/*  23 */     String result = "";
/*     */     try {
/*  25 */       URL realUrl = new URL(url);
/*     */
/*  27 */       URLConnection conn = realUrl.openConnection();
/*     */
/*  29 */       conn.setRequestProperty("Content-Type", "application/json");
/*  30 */       conn.setRequestProperty("connection", "keep-alive");
/*     */
/*     */
/*  33 */       conn.setDoOutput(true);
/*  34 */       conn.setDoInput(true);
/*     */
/*  36 */       conn.setReadTimeout(20000);
/*  37 */       conn.setConnectTimeout(20000);
/*     */
/*     */
/*  40 */       out = new PrintWriter(conn.getOutputStream());
/*     */
/*  42 */       out.write(param);
/*     */
/*  44 */       out.flush();
/*     */
/*  46 */       in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
/*     */       String line;
/*  48 */       while ((line = in.readLine()) != null) {
/*  49 */         result = result + line;
/*     */       }
/*  51 */     } catch (Exception e) {
/*  52 */       logger.info("send http request error :" + e);
/*  53 */       throw e;
/*     */     } finally {
/*     */
/*     */       try {
/*  57 */         if (out != null) {
/*  58 */           out.close();
/*     */         }
/*  60 */         if (in != null) {
/*  61 */           in.close();
/*     */         }
/*  63 */       } catch (IOException ex) {
/*  64 */         throw ex;
/*     */       }
/*     */     }
/*  67 */     return result;
/*     */   }
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */   public static String sendPostFile(String url, Map<String, String> paramMap, byte[] fileByte, String fileName, String fileKey) throws IOException {
/*  75 */     String result = "";
/*     */
/*     */     try {
/*  78 */       String newLine = "\r\n";
/*  79 */       String boundaryPrefix = "--";
/*     */
/*  81 */       String BOUNDARY = "========7d4a6d158c9";
/*     */
/*  83 */       URL realurl = new URL(url);
/*     */
/*  85 */       HttpURLConnection connection = (HttpURLConnection)realurl.openConnection();
/*  86 */       connection.setDoOutput(true);
/*  87 */       connection.setDoInput(true);
/*     */
/*  89 */       connection.setReadTimeout(20000);
/*  90 */       connection.setConnectTimeout(20000);
/*     */
/*  92 */       connection.setUseCaches(false);
/*  93 */       connection.setRequestMethod("POST");
/*  94 */       connection.setRequestProperty("Connection", "Keep-Alive");
/*  95 */       connection.setRequestProperty("Charset", "UTF-8");
/*  96 */       connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
/*     */
/*  98 */       StringBuilder contentBody = null;
/*     */
/* 100 */       OutputStream out = connection.getOutputStream();
/*     */
/*     */
/*     */
/* 104 */       if (paramMap != null) {
/* 105 */         contentBody = new StringBuilder();
/* 106 */         for (String s : paramMap.keySet()) {
/* 107 */           contentBody.append("--" + BOUNDARY);
/* 108 */           contentBody.append("\r\n");
/* 109 */           contentBody.append("Content-Disposition: form-data; name=\"");
/* 110 */           contentBody.append(s);
/* 111 */           contentBody.append("\"").append("\r\n").append("\r\n");
/* 112 */           contentBody.append(paramMap.get(s));
/* 113 */           contentBody.append("\r\n");
/*     */         }
/* 115 */         out.write(contentBody.toString().getBytes());
/*     */       }
/*     */
/*     */
/* 119 */       contentBody = new StringBuilder();
/* 120 */       contentBody.append("--" + BOUNDARY)
/* 121 */         .append("\r\n")
/* 122 */         .append("Content-Disposition:form-data; name=\"")
/* 123 */         .append(fileKey + "\"; ")
/* 124 */         .append("filename=\"")
/* 125 */         .append(fileName + "\"")
/* 126 */         .append("\r\n")
/* 127 */         .append("Content-Type:multipart/form-data")
/* 128 */         .append("\r\n").append("\r\n");
/* 129 */       String boundaryMessage2 = contentBody.toString();
/* 130 */       out.write(boundaryMessage2.getBytes("utf-8"));
/* 131 */       out.write(fileByte, 0, fileByte.length);
/* 132 */       byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
/* 133 */       out.write(endData);
/* 134 */       out.flush();
/* 135 */       out.close();
/*     */
/*     */
/* 138 */       String strLine = "";
/* 139 */       String strResponse = "";
/* 140 */       InputStream in = connection.getInputStream();
/* 141 */       BufferedReader reader = new BufferedReader(new InputStreamReader(in));
/* 142 */       while ((strLine = reader.readLine()) != null)
/*     */       {
/* 144 */         strResponse = strResponse + strLine + "\n";
/*     */       }
/* 146 */       return strResponse;
/* 147 */     } catch (Exception e) {
/* 148 */       logger.info("send http request error :" + e);
/* 149 */       throw e;
/*     */     }
/*     */   }
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */   public static String sendPostParam(String url, String param) throws Exception {
/* 158 */     PrintWriter out = null;
/* 159 */     BufferedReader in = null;
/* 160 */     String result = "";
/*     */     try {
/* 162 */       URL realUrl = new URL(url);
/*     */
/* 164 */       HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
/*     */
/* 166 */       conn.setRequestMethod("POST");
/* 167 */       conn.setRequestProperty("accept", "*/*");
/* 168 */       conn.setRequestProperty("connection", "Keep-Alive");
/* 169 */       conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
/* 170 */       conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
/* 171 */       conn.setRequestProperty("Accept-Charset", "UTF-8");
/*     */
/* 173 */       conn.setDoOutput(true);
/* 174 */       conn.setDoInput(true);
/*     */
/* 176 */       conn.setReadTimeout(20000);
/* 177 */       conn.setConnectTimeout(20000);
/*     */
/*     */
/* 180 */       out = new PrintWriter(conn.getOutputStream());
/*     */
/* 182 */       param = param.replaceAll("\\+", "%2B");
/* 183 */       out.print(param);
/*     */
/* 185 */       out.flush();
/*     */
/* 187 */       in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
/*     */       String line;
/* 189 */       while ((line = in.readLine()) != null) {
/* 190 */         result = result + line;
/*     */       }
/* 192 */     } catch (Exception e) {
/* 193 */       logger.info("send http request error :" + e);
/* 194 */       throw e;
/*     */     } finally {
/*     */
/*     */
/*     */       try {
/* 199 */         if (out != null) {
/* 200 */           out.close();
/*     */         }
/* 202 */         if (in != null) {
/* 203 */           in.close();
/*     */         }
/* 205 */       } catch (IOException ex) {
/* 206 */         ex.printStackTrace();
/*     */       }
/*     */     }
/* 209 */     return result;
/*     */   }
/*     */
/*     */
/*     */
/*     */
/*     */
/*     */   public static byte[] sendPostDownFile(String url, String param) throws IOException {
/* 217 */     PrintWriter out = null;
/* 218 */     InputStream in = null;
/* 219 */     ByteArrayOutputStream output = null;
/*     */     try {
/* 221 */       URL realUrl = new URL(url);
/*     */
/* 223 */       URLConnection conn = realUrl.openConnection();
/*     */
/* 225 */       conn.setRequestProperty("Content-Type", "application/json");
/* 226 */       conn.setRequestProperty("connection", "keep-alive");
/*     */
/* 228 */       conn.setDoOutput(true);
/* 229 */       conn.setDoInput(true);
/*     */
/* 231 */       conn.setReadTimeout(20000);
/* 232 */       conn.setConnectTimeout(20000);
/*     */
/*     */
/* 235 */       out = new PrintWriter(conn.getOutputStream());
/*     */
/* 237 */       out.write(param);
/*     */
/* 239 */       out.flush();
/*     */
/*     */
/* 242 */       in = conn.getInputStream();
/* 243 */       output = new ByteArrayOutputStream();
/* 244 */       byte[] buffer = new byte[4096];
/* 245 */       int n = 0;
/* 246 */       while (-1 != (n = in.read(buffer))) {
/* 247 */         output.write(buffer, 0, n);
/*     */       }
/* 249 */       return output.toByteArray();
/* 250 */     } catch (Exception e) {
/* 251 */       logger.info("send http request error :" + e);
/* 252 */       throw e;
/*     */     } finally {
/*     */
/*     */       try {
/* 256 */         if (out != null) {
/* 257 */           out.close();
/*     */         }
/* 259 */         if (in != null) {
/* 260 */           in.close();
/*     */         }
/* 262 */         if (output != null) {
/* 263 */           output.close();
/*     */         }
/* 265 */       } catch (IOException ex) {
/* 266 */         throw ex;
/*     */       }
/*     */     }
/*     */   }


/*     */ }


/* Location:              D:\repository\io\github\ysgatesdk\yspay-opensdk-java\1.0.1\yspay-opensdk-java-1.0.1.jar!\com\eptok\yspay\opensdkjav\\util\HttpClientUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */
